package com.jieli.bt.att.tool.ble.model;


import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothProfile;
import android.os.Parcel;
import android.os.Parcelable;

import com.jieli.bt.att.util.BluetoothUtil;
import com.jieli.bt.att.util.CHexConver;

import java.util.Objects;

/**
 * BLE 设备扫描信息
 * <p>
 * Created by zqjasonzhong on 2018/10/17.
 */

public class ScanDeviceInfo implements Parcelable {
    /**
     * 蓝牙设备
     */
    private final BluetoothDevice device;
    /**
     * 信号强度
     */
    private int rssi;
    /**
     * 是否允许连接
     * <p>说明: 默认允许连接，特殊情况不允许</p>
     */
    private boolean isEnableConnect = true;
    /**
     * 原始数据
     */
    private byte[] rawData;
    /**
     * 连接状态
     */
    private int connection = BluetoothProfile.STATE_DISCONNECTED;

    public ScanDeviceInfo(BluetoothDevice device) {
        this.device = device;
    }

    protected ScanDeviceInfo(Parcel in) {
        device = in.readParcelable(BluetoothDevice.class.getClassLoader());
        rawData = in.createByteArray();
        rssi = in.readInt();
        isEnableConnect = in.readByte() != 0;
        connection = in.readInt();
    }

    public static final Creator<ScanDeviceInfo> CREATOR = new Creator<ScanDeviceInfo>() {
        @Override
        public ScanDeviceInfo createFromParcel(Parcel in) {
            return new ScanDeviceInfo(in);
        }

        @Override
        public ScanDeviceInfo[] newArray(int size) {
            return new ScanDeviceInfo[size];
        }
    };

    public BluetoothDevice getDevice() {
        return device;
    }

    public int getRssi() {
        return rssi;
    }

    public ScanDeviceInfo setRssi(int rssi) {
        this.rssi = rssi;
        return this;
    }

    public boolean isEnableConnect() {
        return isEnableConnect;
    }

    public ScanDeviceInfo setEnableConnect(boolean enableConnect) {
        isEnableConnect = enableConnect;
        return this;
    }

    public byte[] getRawData() {
        return rawData;
    }

    public ScanDeviceInfo setRawData(byte[] rawData) {
        this.rawData = rawData;
        return this;
    }

    public int getConnection() {
        return connection;
    }

    public ScanDeviceInfo setConnection(int connection) {
        this.connection = connection;
        return this;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeParcelable(device, flags);
        dest.writeByteArray(rawData);
        dest.writeInt(rssi);
        dest.writeByte((byte) (isEnableConnect ? 1 : 0));
        dest.writeInt(connection);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        ScanDeviceInfo that = (ScanDeviceInfo) o;
        return Objects.equals(device, that.device);
    }

    @Override
    public int hashCode() {
        return Objects.hashCode(device);
    }

    @Override
    public String toString() {
        return "ScanDeviceInfo{" +
                "device=" + device +
                ", rssi=" + rssi +
                ", isEnableConnect=" + isEnableConnect +
                ", connection=" + BluetoothUtil.printConnectionState(connection) +
                ",\nrawData=" + CHexConver.byte2HexStr(rawData) +
                '}';
    }
}
